package com.qifenqian.turbot.module.ui.loginetc

import android.content.Context
import android.content.Intent
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseActivity
import com.qifenqian.turbot.config.Constant
import com.qifenqian.turbot.config.Constant.TAG_ACTIVITY_COLLECTOR_REGISTER
import com.qifenqian.turbot.config.TurbotEnum
import com.qifenqian.turbot.module.contract.AccountContract
import com.qifenqian.turbot.module.presenter.AccountPresenter
import com.qifenqian.turbot.util.ActivityCollector
import com.qifenqian.turbot.util.SendSmsUtil
import com.qifenqian.turbot.util.StringUtils
import kotlinx.android.synthetic.main.activity_register.*
import org.jetbrains.anko.toast

/**
 * @author H
 * @createDate 2018/12/17 9:29
 * @description 注册页面1
 * @see
 * @since
 */
class RegisterActivity : BaseActivity<AccountPresenter, AccountContract.View>(), AccountContract.View {

    companion object {
        fun startThis(context: Context) {
            val intent = Intent(context, RegisterActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun bindLayoutId(): Int {
        return R.layout.activity_register
    }

    override fun bindPresenter(): AccountPresenter? {
        return AccountPresenter(this, this)
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityCollector.removeActivity(this, TAG_ACTIVITY_COLLECTOR_REGISTER)
    }

    override fun initial() {
        ActivityCollector.addActivity(this, TAG_ACTIVITY_COLLECTOR_REGISTER)
        btn_register_get_code.setOnClickListener {
            if (et_register_account.text.toString().isEmpty()) {
                toast("请输入手机号码")
                return@setOnClickListener
            }
            if (!StringUtils.isPhoneNumber(et_register_account.text.toString())) {
                toast("手机号码格式错误")
                return@setOnClickListener
            }
            mPresenter!!.sendSmsCode(TurbotEnum.SMS_REGISTER.type)
        }
        btn_register_register.setOnClickListener {
            if (et_register_account.text.toString().isEmpty()) {
                toast("请输入手机号码")
                return@setOnClickListener
            }
            if (et_register_sms.text.toString().isEmpty()) {
                toast("请输入验证码")
                return@setOnClickListener
            }
            if (et_register_password.text.toString().isEmpty()) {
                toast("请输入密码")
                return@setOnClickListener
            }
//            if (!cb_register_check.isChecked) {
//                toast("请同意《商家服务协议》")
//                return@setOnClickListener
//            }
            // TODO: 2018/12/17 10:03 注册 ----->
            mPresenter!!.register(et_register_account.text.toString(), et_register_sms.text.toString(), et_register_password.text.toString())
//            TODO("移除")
            IdentityVerifyActivity.startThis(this, et_register_account.text.toString())
        }
//        tv_register_protocol.setOnClickListener {
//            // TODO: 2018/12/17 11:45 跳转到注册协议 ----->
//        }
        btn_register_login.setOnClickListener {
            LoginActivity.startThis(this)
        }
    }

    override fun sendSmsCodeResult(isSuccess: Boolean, code: Int, msg: String?) {
        if (isSuccess) {
            //开启倒计时
            SendSmsUtil.beginCountdown(this, Constant.COUNTDOWN_TIME, btn_register_get_code)
            toast("获取验证码成功")
        } else {
            //请求验证码失败则关闭倒计时
            SendSmsUtil.disposeCountdown()
//        btn_login_get_code.text = "重新发送"
//        btn_login_get_code.setTextColor(Color.parseColor("#ff0080ff"))
//        btn_login_get_code.isClickable = true
            toast(msg!!)
        }
    }

    override fun registerResult(isSuccess: Boolean, code: Int, msg: String?) {
        if (isSuccess) {
            // DO: 2018/12/17 11:43 注册成功,跳转到下个界面 ----->
            IdentityVerifyActivity.startThis(this, et_register_account.text.toString())
        } else {
            toast(msg!!)
        }
    }
}
